package com.yododo.deploy;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.yododo.deploy.util.CopyListUtil;
import com.yododo.deploy.util.FileTree;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	    String srcDir = "D:/Projects/tour/";
        String releaseDir = "E:/deploy";
        String testDir = "E:/deployTest";
		
		File releaseFile = new File(releaseDir);
		if (!releaseFile.exists()) {
		    releaseFile.mkdirs();
		}
		
		File testFile = new File(testDir);
		if (!testFile.exists()) {
			testFile.mkdirs();
		}
		
		srcDir = srcDir.replace("\\", "/");
		releaseDir = releaseDir.replace("\\", "/");
		testDir = testDir.replace("\\", "/");
		
		List<String> copyList = CopyListUtil.getCopyList(srcDir);
		
		copyDeployFiles(copyList, srcDir, releaseDir);
		
		copyDeployFiles(copyList, srcDir, testDir);
		
		checkEnvDeployFiles(srcDir, releaseDir, testDir);
	}

    private static void copyDeployFiles(List<String> copyList, String srcDir, String targetDir) {
        System.out.println("Copy start.");
        File cleanDir = new File(targetDir);
		try {
			FileUtils.cleanDirectory(cleanDir);
		} catch (IOException e1) {
			System.out.println("Clean " + targetDir + " error");
		}
		
		for (String string : copyList) {
			String fileTmp = string;
			
			if (fileTmp.startsWith("/src")) {
				fileTmp = fileTmp.replaceAll(".java$", ".class");
			}
			
			String srcFileTmp = fileTmp;
			String tarFileTmp = fileTmp;
			
			if (srcFileTmp.startsWith("/src")) {
				srcFileTmp = srcFileTmp.replaceFirst("^/src", "/web/WEB-INF/classes");
			}
			
			
			if (tarFileTmp.startsWith("/src")) {
				tarFileTmp = tarFileTmp.replaceFirst("^/src", "/WEB-INF/classes");
			}
			
			
			if (tarFileTmp.startsWith("/web")) {
				tarFileTmp = tarFileTmp.replaceFirst("^/web", "");
			}
			
			
			srcFileTmp = srcDir + srcFileTmp;
			File srcFile = new File(srcFileTmp);
			
			tarFileTmp = targetDir + tarFileTmp;
			File targetFile = new File(tarFileTmp);
			try {
			    if (srcFile.isDirectory()) {
			        System.out.println("ignore dir " + srcFile);
			        System.out.println();
//			        FileUtils.copyDirectory(srcFile, targetFile);
//			        System.out.println(srcFileTmp + " copying...");
//	                System.out.println("           " + targetFile + " successed.");
//	                System.out.println();
//	                FileUtils.deleteDirectory(new File(tarFileTmp+ "/.svn"));
	                continue;
			    }
			    
				FileUtils.copyFile(srcFile, targetFile);
				
				System.out.println(srcFileTmp + " copying...");
				System.out.println("           " + targetFile + " successed.");
				System.out.println();
				
				if (srcFileTmp.indexOf(".class") != -1) {
				    int lastIndexOf = srcFileTmp.lastIndexOf("/");
				    String copyFileDir = srcFileTmp.substring(0, lastIndexOf);
				    
				    final String fileFilter = srcFileTmp.substring(lastIndexOf + 1, srcFileTmp.indexOf(".class")) + "$";
				    File copyFileDirFile = new File(copyFileDir);
				    File[] listFiles = copyFileDirFile.listFiles(new FilenameFilter() {

                        public boolean accept(File dir, String name) {
                            if (name.indexOf(fileFilter) != -1) {
                                return true;
                            }
                            return false;
                        }
                    });
				    for (File file : listFiles) {
				        String replaceFile = file.getPath().replaceAll("\\\\", "/").replace(srcDir + "/web", targetDir);
				        File $1targetFile = new File(replaceFile);
                        FileUtils.copyFile(file, $1targetFile);
                        
                        System.out.println(file + " copying...");
                        System.out.println("           " + replaceFile + " successed.");
                        System.out.println();
                    }
				    
                }
			} catch (IOException e) {
				System.out.println("***" + srcFile + " failed.");
				e.printStackTrace();
			}
		}
		System.out.println("Copy finished.");
		
		FileTree tree = new FileTree(targetDir);  
        tree.tree();
    }
    
    private static void checkEnvDeployFiles(String srcDir, String targetDir, String targetTestDir) {
    	checkEnvDeployFiles("/WEB-INF/classes/ehcache.xml", "/src/ehcache.online.xml", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/classes/ehcache.xml",  "/src/ehcache.test.xml", srcDir, targetTestDir);
    	
    	checkEnvDeployFiles("/WEB-INF/classes/ehcache-all.xml", "/src/ehcache-all.online.xml", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/classes/ehcache-all.xml", "/src/ehcache-all.test.xml", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/classes/log4j.properties", "/src/log4j.online.properties", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/classes/log4j.properties", "/src/log4j.test.properties", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/config.properties", "/web/WEB-INF/conf/config.properties.online", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/config.properties", "/web/WEB-INF/conf/config.properties.test", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/jdbc.properties", "/web/WEB-INF/conf/jdbc.properties.online", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/jdbc.properties", "/web/WEB-INF/conf/jdbc.properties.test", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/FacadeContext.xml", "/web/WEB-INF/conf/FacadeContext.online.xml", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/FacadeContext.xml", "/web/WEB-INF/conf/FacadeContext.test.xml", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/mail.properties", "/web/WEB-INF/conf/mail.properties.online", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/mail.properties", "/web/WEB-INF/conf/mail.properties.test", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/SearchEngineContext.xml", "/web/WEB-INF/conf/SearchEngineContext.online.xml", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/SearchEngineContext.xml", "/web/WEB-INF/conf/SearchEngineContext.test.xml", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/WebserviceContext.properties", "/web/WEB-INF/conf/WebserviceContext.properties.online", srcDir, targetDir);

    	checkEnvDeployFiles("/WEB-INF/conf/TaobaoAuth.properties", "/web/WEB-INF/conf/TaobaoAuth.properties.online", srcDir, targetDir);

    	checkEnvDeployFiles("/WEB-INF/conf/qunarConfig.properties", "/web/WEB-INF/conf/qunarConfig.properties.online", srcDir, targetDir);
    	
    	checkEnvDeployFiles("/WEB-INF/conf/sms.properties", "/web/WEB-INF/conf/sms.properties.online", srcDir, targetDir);
    	checkEnvDeployFiles("/WEB-INF/conf/sms.properties", "/web/WEB-INF/conf/sms.properties.test", srcDir, targetTestDir);

    	checkEnvDeployFiles("/WEB-INF/conf/redis.properties", "/web/WEB-INF/conf/redis.properties.online", srcDir, targetDir);

    	checkEnvDeployFiles("/WEB-INF/classes/config.properties", "/src/config.online.properties", srcDir, targetDir);
    }
    
    private static void checkEnvDeployFiles(String envFile, String srcEnvFile, String srcDir, String targetDir) {
    	File targetEnvFile = new File(targetDir + envFile);
		if(targetEnvFile.exists()) {
    		File srcFile = new File(srcDir + srcEnvFile);
			try {
				FileUtils.copyFile(srcFile, targetEnvFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
            System.out.println("override envFile: " + srcFile.getAbsolutePath() + "->" + targetEnvFile.getAbsolutePath() + " success.");
    	}
    }

}
